package com.ccrego.haiyi.account.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ccrego.haiyi.R;
import com.ccrego.haiyi.account.AccountManager;
import com.ccrego.haiyi.account.UrlCenter;
import com.ccrego.haiyi.common.http.MyHttpRequest;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.customviews.edittext.PasswordEditText;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.MD5Util;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

public class H5ModifyPassWordActivity extends BaseActivity {

    @Bind(R.id.itetH5OldPassword)
    PasswordEditText mItetH5OldPassword;
    @Bind(R.id.itetH5Password)
    PasswordEditText mItetH5Password;
    @Bind(R.id.itetH5Password_again)
    PasswordEditText mItetH5PasswordAgain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.updata);
        getCustomToolbar().addLeftImageButton(R.mipmap.icon_back1, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, H5ModifyPassWordActivity.class);
        activity.startActivity(intent);
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_h5_modify_pass_word, null);
    }

    @OnClick(R.id.btn_H5_next)
    public void onViewClicked() {
        if (TextUtils.isEmpty(mItetH5OldPassword.getEditText().toString())) {
            EasyToast.showToast(getApplicationContext(),"请输入旧密码");
            return;
        }
        if (TextUtils.isEmpty(mItetH5Password.getEditText().toString())) {
            EasyToast.showToast(getApplicationContext(),"请输入新密码");
            return;
        }
        if (TextUtils.isEmpty(mItetH5PasswordAgain.getEditText().toString())) {
            EasyToast.showToast(getApplicationContext(),"请输入新密码");
            return;
        }
        if(!mItetH5Password.getEditText().toString().equals(mItetH5PasswordAgain.getEditText().toString())){
            EasyToast.showToast(getApplicationContext(),"两次密码不一致");
            return;
        }
        MyHttpRequest myHttpRequest = new MyHttpRequest(this);
        JSONObject jobj = new JSONObject();
        try {
            jobj.put("password",MD5Util.getMd5(mItetH5OldPassword.getEditText().toString()));
            jobj.put("new_password",MD5Util.getMd5(mItetH5PasswordAgain.getEditText().toString()));
            myHttpRequest.put(UrlCenter.USER_PASSWORD, jobj, new DataRequestListener<String>(String.class) {
                @Override
                public void onStart(Context context, String url) {
                    super.onStart(context, url);
                }

                @Override
                public void onSuccess(String o) {
                    super.onSuccess(o);
                    finish();
                    EasyToast.showToast(getApplicationContext(),"修改密码成功");
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    EasyToast.showToast(getApplicationContext(),"旧密码错误");
                }
            });



        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
